keep = ls()

##############################
#Get Indiana 1860 Census Data#
##############################

#Get White Male Residents of counties > 10 years old in 1860
census_1860 = fread(paste0(ipums_census_path,'/indiana_1860.txt')) %>%
  .[ResidenceCounty %in%  c("Bartholomew", 'Boone', "Johnson", "Hamilton",
                            "Hendricks", "Henry", "Montgomery",
                            "Morgan", "Vermillion")] %>%
  .[Gender %in% "Male" &
      Race %in% c("White", "") &
      as.numeric(ResidenceAge) > 10]

#Clean names
census_1860[, paste("match", 
                    c("first", "middle", "last", "first_clean"), 
                    sep = "_") := clean_names(first = Given,
                                              middle = NULL,
                                              last = Surname
                    )]

census_1860[, birth_year := 1860 - as.numeric(ResidenceAge)]


#Census data to match
census_1860_match = census_1860[, list(mpcid, 
                                       census_county = ResidenceCounty,
                                       match_first,
                                       match_middle,
                                       match_last,
                                       match_first_clean,
                                       match_middle_init = match_middle %>% str_sub(1,1),
                                       first_sound = metaphone(match_first),
                                       last_sound = metaphone(match_last),
                                       birth_year,
                                       birth_place = BirthPlace)
                                ]

#Save cleaned census data
fwrite(census_1860_match, "./cleaned/census_1860_to_match.csv")

#Cleanup
rm(list = setdiff(ls(), c(keep, 'keep')))
gc()
